package com.tiancheng.trade.merchant.util.Queue;

import com.alibaba.fastjson2.JSON;
import com.tiancheng.trade.merchant.dto.message.MerchantStatusMsg;
import com.tiancheng.trade.merchant.enums.MerchantApplyStatusEnums;
import com.tiancheng.trade.merchant.service.IMerchantPayRegService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangjun
 * @description 商户进件消息的处理者
 * @date 2024/10/14 23:39
 */
@Component
@RabbitListener(queues = "tread.merchant.register")
@Slf4j
public class MerchantRegisterReceiver {

    @Autowired
    private IMerchantPayRegService merchantPayRegService;

    @RabbitHandler
    public void handle(String merchantPayRegJSON) throws Exception {
        log.info("商户进件状态消息处理，message:{}", merchantPayRegJSON);
        MerchantStatusMsg merchantPayReg = JSON.parseObject(merchantPayRegJSON, MerchantStatusMsg.class);
        //merchantPayReg.getApplyStatus()
//        ManualSettingChannelInfoReq params = new ManualSettingChannelInfoReq();
//        params.setId(merchantPayReg.getMerchantPayRegId());
//        params.setApplyStatus(1);
//        //TODO 这里以后要取银联产生的商户号
//        params.setChannelMerNo("89853057399799G");
        Map<String, Object> message = new HashMap<>();
        message.put("ums_reg_id", merchantPayReg.getChannelRegId());
        if (merchantPayReg.getApplyStatus().equals(Integer.parseInt(MerchantApplyStatusEnums.success.getCode()))) {
            message.put("apply_status", "03");
            message.put("mer_no", "89853057399799J");
            message.put("company_no", "89853057399799K");

        } else if (merchantPayReg.getApplyStatus().equals(Integer.parseInt(MerchantApplyStatusEnums.final_error.getCode()))) {
            message.put("apply_status", "04");
        } else if (merchantPayReg.getApplyStatus().equals(Integer.parseInt(MerchantApplyStatusEnums.exception.getCode()))) {
            message.put("apply_status", "28");
        }
        merchantPayRegService.handleUmsMsgAndNotifyBusiness(message);

    }
}
